package xin.altitude.delaytask.config;

import cn.hutool.core.thread.ThreadUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xin.altitude.delaytask.domain.BuOrder;
import xin.altitude.delaytask.service.IOrderService;

import java.io.IOException;
import java.util.Objects;


/**
 * @author explore
 * @since 2022/03/04 18:54
 **/
@Component
@Slf4j
public class OrderReceiver {
    @Autowired
    private IOrderService orderService;
    
    /**
     * 模拟监听消息队列
     *
     * @param channel
     * @param message
     * @param order
     */
    @RabbitListener(queues = RabbitmqConfig.DELAY_QUEUE_NAME)
    public void consumeNode01(Channel channel, Message message, BuOrder order) throws IOException {
        if (Objects.equals(0, order.getOrderStatus())) {
            /* 修改订单状态，设置为关闭状态 */
            orderService.updateById(new BuOrder(order.getOrderId(), -1));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info(String.format("消费者节点01消费编号为【%s】的消息", order.getOrderId()));
        }
    }
    
    /**
     * 模拟监听消息队列
     *
     * @param channel
     * @param message
     * @param order
     */
    @RabbitListener(queues = RabbitmqConfig.DELAY_QUEUE_NAME)
    public void consumeNode02(Channel channel, Message message, BuOrder order) throws IOException {
        if (Objects.equals(0, order.getOrderStatus())) {
            /* 修改订单状态，设置为关闭状态 */
            orderService.updateById(new BuOrder(order.getOrderId(), -1));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info(String.format("消费者节点02消费编号为【%s】的消息", order.getOrderId()));
        }
    }
}
